import api from './api';

interface LoginData {
    username: string;
    password: string;
    captcha: string;
}

interface LoginResponse {
    token: string;
    expiresIn: number;
}

export const login = async (data: LoginData): Promise<LoginResponse> => {
    // 模拟登录验证
    if (data.username === 'admin' && data.password === '123456' && data.captcha) {
        return {
            token: 'mock-token-' + Math.random().toString(36).substring(2),
            expiresIn: 3600
        };
    }
    throw new Error('用户名或密码错误');
};

export const verifyToken = async (): Promise<boolean> => {
    // 模拟token验证
    return !!localStorage.getItem('token');
};

export const getCaptcha = async (): Promise<string> => {
    // 模拟验证码 - 这里返回一个简单的文本验证码
    return 'data:image/svg+xml;base64,' + btoa(
        `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="40" viewBox="0,0,100,40">
      <rect width="100%" height="100%" fill="#eee"/>
      <text x="50%" y="50%" font-family="Arial" font-size="20" text-anchor="middle" dominant-baseline="middle">${Math.random().toString(36).substring(2, 6)}</text>
    </svg>`
    );
};